﻿using NFine.Domain.Entity.BusinessManage;
using NFine.Domain.IRepository.BusinessManage;
using NFine.Repository.BusinessManage;
using NFine.Code;
using System.Collections.Generic;
using System.Linq;
using NFine.Domain.IRepository.SystemManage;
using NFine.Repository.SystemManage;

namespace NFine.Application.BusinessManage
{
    public class D_GroupMemberApp
    {
        private ID_GroupMemberRepository service = new D_GroupMemberRepository();
        private Domain.IRepository.SystemManage.IUserRepository userservice = new Repository.SystemManage.UserRepository();

        //public List<D_GroupMemberEntity> GetList(Pagination pagination, string keyword)
        //{
        //    var expression = ExtLinq.True<D_GroupMemberEntity>();
        //    if (!string.IsNullOrEmpty(keyword))
        //    {
        //        expression = expression.And(t => t.F_Name.Contains(keyword));
        //        expression = expression.Or(t => t.F_Summary.Contains(keyword));
        //        expression = expression.Or(t => t.F_Spec.Contains(keyword));
        //    }
        //    return service.FindList(expression, pagination);
        //}

        public List<D_GroupMemberEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public D_GroupMemberEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.ID == keyValue);
        }
        public void SubmitForm(D_GroupMemberEntity goodEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                goodEntity.Modify(keyValue);
                service.Update(goodEntity);
            }
            else
            {
                goodEntity.Create();
                service.Insert(goodEntity);
            }
        }

        public List<Domain.ViewModel.GroupMembersModel> GetGroupMembers(string account)
        {
            IUserRepository userservice = new UserRepository();
            ID_GroupRepository groupservice = new D_GroupRepository();

            var user = userservice.FindEntity(m => m.F_Account == account);
            if (user != null)
            {
                var list = service.GetGroupMembers(user.CurrentGroupID);
                return list;
            }
            else
            {
                return null;
            }
        }

        public D_GroupMemberEntity GetEntity(string userId, string groupId)
        {
            var expression = ExtLinq.True<D_GroupMemberEntity>();
            expression = expression.And(t => t.MemberID.Equals(userId));
            expression = expression.And(t => t.GroupID.Equals(groupId));
            return service.FindEntity(expression);
        }
    }
}
